//
//  GXtabbarController.m
//  eyuebus_pro
//
//  Created by 刘方亮 on 2017/5/2.
//  Copyright © 2017年 谷传续. All rights reserved.
//

#import "GXtabbarController.h"
#import "ShouYeViewController.h"
#import "KeHuViewController.h"
#import "WoDeViewController.h"
#import "GXBaseNavController.h"
//#import "FuJinViewController.h"
#import "ShenPiViewController.h"
@interface GXtabbarController ()

@end

@implementation GXtabbarController


- (instancetype)init
{
    //1.初始化父类
    self = [super init];
    //2.判断父类是否初始化成功
    if(self)
    {
        //3.初始化子类
        //设置属性的值
          [self createSubCtrls];
    }
    //4.返回地址
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 方式三：适配iOS13
//    if (@available(iOS 13.0, *)) {
//        // iOS13 及以上
//        self.tabBar.tintColor = mainColor;
//        self.tabBar.unselectedItemTintColor = rgba(51,51,51,1);
//
//    }
//    else {
//        // iOS13 以下
//        UITabBarItem *item = [UITabBarItem appearance];
//        [item setTitleTextAttributes:@{ NSForegroundColorAttributeName:rgba(51,51,51,1)} forState:UIControlStateNormal];
//        [item setTitleTextAttributes:@{ NSForegroundColorAttributeName:mainColor} forState:UIControlStateSelected];
//    }
    
    
    
    [self custonTabBar];
}


- (void)custonTabBar {
    NSDictionary *attrNormal = @{NSFontAttributeName:[UIFont systemFontOfSize:11],NSForegroundColorAttributeName:rgba(182, 193, 211, 1)};
    NSDictionary *attrSelect = [NSDictionary dictionary];
    UITabBar *tabBar = [UITabBar appearance];
    //ios 13 之后需要这样设置才有效
    if (@available(iOS 13.0, *)) {
        attrSelect = @{NSFontAttributeName:[UIFont systemFontOfSize:11],NSForegroundColorAttributeName:mainColor};
        UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc]init];
        //设置tabar背景色
        tabBarAppearance.backgroundColor = [UIColor secondarySystemGroupedBackgroundColor];
        tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = attrNormal;
        tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = attrSelect;
        
        CGRect rect = CGRectMake(0, 0, ScreenWidth, 1);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context,
                                           [UIColor whiteColor].CGColor);
        CGContextFillRect(context, rect);
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        
        tabBarAppearance.shadowImage = img;
        
        
        //必须要加上这两句
        tabBar.standardAppearance = tabBarAppearance;
        if (@available(iOS 15.0, *)) {
            tabBar.scrollEdgeAppearance = tabBarAppearance;
        } else {
            // Fallback on earlier versions
        }
    } else {
        attrSelect = @{NSFontAttributeName:[UIFont systemFontOfSize:11],NSForegroundColorAttributeName:mainColor};
        UITabBarItem *tbItem = [UITabBarItem appearance];
        [tbItem setTitleTextAttributes:attrNormal forState:UIControlStateNormal];
        [tbItem setTitleTextAttributes:attrSelect forState:UIControlStateSelected];
        [tabBar setBarTintColor:[UIColor whiteColor]];  //tabBar的背景色
    }
    tabBar.translucent = NO;     //translucent: 半透明的
 
}





//是否跟随屏幕旋转
-(BOOL)shouldAutorotate{
    return self.selectedViewController.shouldAutorotate;
}
//支持旋转的方向有哪些
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}
//控制 vc present进来的横竖屏和进入方向 ，支持的旋转方向必须包含改返回值的方向 （详细的说明见下文）
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}


- (void)createSubCtrls{
        

    GXBaseNavController *firstNav = [[GXBaseNavController alloc] initWithRootViewController:[[ShouYeViewController alloc] init]];
    GXBaseNavController *secondNav = [[GXBaseNavController alloc] initWithRootViewController:[[KeHuViewController alloc] init]];
//    GXBaseNavController *thirdNav = [[GXBaseNavController alloc] initWithRootViewController:[[FuJinViewController alloc] init]];
    GXBaseNavController *four = [[GXBaseNavController alloc] initWithRootViewController:[[ShenPiViewController alloc] init]];
    
    GXBaseNavController *five = [[GXBaseNavController alloc] initWithRootViewController:[[WoDeViewController alloc] init]];

    
//    self.viewControllers = @[firstNav,secondNav,thirdNav,four,five];
    
    self.viewControllers = @[firstNav,secondNav,four,five];
    
  
    
    [self setTabBarItems:self];
}


- (void)setTabBarItems:(UITabBarController*)tabBarVC{

//    NSArray *titles = @[@"首页",@"客户", @"附近",@"审批", @"我的"];
//    NSArray *normalImages = @[@"首页",@"客户", @"附近",@"审批", @"我的"];
//    NSArray *highlightImages = @[@"首页选中",@"客户选中", @"附近选中",@"审批选中", @"我的选中"];
//    [tabBarVC.tabBar.items enumerateObjectsUsingBlock:^(UITabBarItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        obj.title = titles[idx];
//        obj.image = [[UIImage imageNamed:normalImages[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//        obj.selectedImage = [[UIImage imageNamed:highlightImages[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    }];
    
    NSArray *titles = @[@"首页",@"客户",@"审批", @"我的"];
    NSArray *normalImages = @[@"首页",@"客户", @"审批", @"我的"];
    NSArray *highlightImages = @[@"首页选中",@"客户选中", @"审批选中", @"我的选中"];
    [tabBarVC.tabBar.items enumerateObjectsUsingBlock:^(UITabBarItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.title = titles[idx];
        obj.image = [[UIImage imageNamed:normalImages[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        obj.selectedImage = [[UIImage imageNamed:highlightImages[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }];
}




- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{
    vc.navigationItem.title = title;
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    GXBaseNavController *nav = [[GXBaseNavController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}




//取消图片渲染
- (UIImage *)cancelRenderingMode:(UIImage *)image{
    
    image=[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    return image;
}



@end
